package cn.sirun.com.fca.personal.center.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.recycler.flow.BaseDisposableRecyclerPresenter;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import java.util.List;

import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.model.login.PersonalInfoRequest;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActionConst;
import cn.sirun.com.fca.personal.center.loader.PersonalCenterLoader;
import cn.sirun.com.fca.personal.center.model.response.CityResponse;
import cn.sirun.com.fca.personal.center.model.service.PersonalCenterServices;
import cn.sirun.com.fca.personal.center.presenter.IPersonalCenterPresenter;
import cn.sirun.com.fca.personal.center.presenter.PersonalCenterFlowPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class PersonalCenterPresenter extends PersonalCenterFlowPresenter<PersonalCenterLoader>
        implements IPersonalCenterPresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected PersonalCenterLoader createLoader() {
        return new PersonalCenterLoader();
    }


//    @Override
//    public void refreshData() {
//        getLoader().refreshData();
//    }

    @Override
    public void getPersonalInfo(PersonalInfoRequest request) {
        getLoader().getPersonalInfo(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<PersonalInfo>>() {

                    @Override
                    public void onNext(BaseResponse<PersonalInfo> personalInfoResponse) {
                        super.onNext(personalInfoResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_PERSONAL_INFO_RESPONSE,
                                        personalInfoResponse);
                    }
                });
    }

    @Override
    public void getCarInfo(String din) {
        getLoader().getCarInfo(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<CarInfoDetailResponse>>() {

                    @Override
                    public void onNext(BaseResponse<CarInfoDetailResponse> carInfoResponse) {
                        super.onNext(carInfoResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_CAR_INFO_RESPONSE,
                                        carInfoResponse);
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        PersonalCenterServices.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
